package com.hqd.ch03.v48.format.support;

import com.hqd.ch03.utils.ClassUtils;
import com.hqd.ch03.v48.beans.converter.support.DefaultConversionService;
import com.hqd.ch03.v48.config.StringValueResolver;
import com.hqd.ch03.v48.format.FormatterRegistry;

public class DefaultFormattingConversionService extends FormattingConversionService {

    private static final boolean jsr354Present;

    private static final boolean jodaTimePresent;

    static {
        ClassLoader classLoader = DefaultFormattingConversionService.class.getClassLoader();
        jsr354Present = ClassUtils.isPresent("javax.money.MonetaryAmount", classLoader);
        jodaTimePresent = ClassUtils.isPresent("org.joda.time.YearMonth", classLoader);
    }

    /**
     * Create a new {@code DefaultFormattingConversionService} with the set of
     * {@linkplain DefaultConversionService#addDefaultConverters default converters} and
     * {@linkplain #addDefaultFormatters default formatters}.
     */
    public DefaultFormattingConversionService() {
        this(null, true);
    }

    /**
     * Create a new {@code DefaultFormattingConversionService} with the set of
     * {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
     * based on the value of {@code registerDefaultFormatters}, the set of
     * {@linkplain #addDefaultFormatters default formatters}.
     *
     * @param registerDefaultFormatters whether to register default formatters
     */
    public DefaultFormattingConversionService(boolean registerDefaultFormatters) {
        this(null, registerDefaultFormatters);
    }

    /**
     * Create a new {@code DefaultFormattingConversionService} with the set of
     * {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
     * based on the value of {@code registerDefaultFormatters}, the set of
     * {@linkplain #addDefaultFormatters default formatters}.
     *
     * @param embeddedValueResolver     delegated to {@link #setEmbeddedValueResolver(StringValueResolver)}
     *                                  prior to calling {@link #addDefaultFormatters}.
     * @param registerDefaultFormatters whether to register default formatters
     */
    public DefaultFormattingConversionService(
            StringValueResolver embeddedValueResolver, boolean registerDefaultFormatters) {

        if (embeddedValueResolver != null) {
            setEmbeddedValueResolver(embeddedValueResolver);
        }
        DefaultConversionService.addDefaultConverters(this);
        if (registerDefaultFormatters) {
            addDefaultFormatters(this);
        }
    }


    /**
     * Add formatters appropriate for most environments: including number formatters,
     * JSR-354 Money &amp; Currency formatters, JSR-310 Date-Time and/or Joda-Time formatters,
     * depending on the presence of the corresponding API on the classpath.
     *
     * @param formatterRegistry the service to register default formatters with
     */
    @SuppressWarnings("deprecation")
    public static void addDefaultFormatters(FormatterRegistry formatterRegistry) {
    }

}